package day52.pet.controller;

import day52.basic.domain.dto.ConfirmOrderDto;
import day52.basic.util.CurrentLoginInfoUtil;
import day52.org.mapper.EmployeeMapper;
import day52.pet.service.IOrderPetAcquisitionService;
import day52.pet.domain.OrderPetAcquisition;
import day52.pet.query.OrderPetAcquisitionQuery;
import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import day52.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {
    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;
    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 保存和修改公用的
     * @param orderPetAcquisition  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){

        if( orderPetAcquisition.getId()!=null){
            orderPetAcquisitionService.update(orderPetAcquisition);
        }else{
            orderPetAcquisitionService.add(orderPetAcquisition);
        }
        return AjaxResult.me();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        orderPetAcquisitionService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderPetAcquisition get(@PathVariable("id")Long id) {

        return orderPetAcquisitionService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderPetAcquisition> list(){

        return orderPetAcquisitionService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderPetAcquisition> json(@RequestBody OrderPetAcquisitionQuery query) {

        return orderPetAcquisitionService.pageQuery(query);
    }

    /**
     * 查看个人收购订单
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/emp_list")
    public PageList<OrderPetAcquisition> empList(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) throws Exception {
        Logininfo lf = CurrentLoginInfoUtil.currentLoginInfo(request);
        Long empId = employeeMapper.findByLoginInfoId(lf.getId()).getId();
        query.setEmpId(empId);
        return orderPetAcquisitionService.pageQuery(query);
    }

    /**
     * 确认订单
     * @param dto
     * @return
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody ConfirmOrderDto dto){
        return orderPetAcquisitionService.confirm(dto);
    }

    /**
     * 取消订单
     */
    @DeleteMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id){
        return orderPetAcquisitionService.cancel(id);
    }


}
